package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.UserComplaintsRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Usercomplaints;
import com.tiantian.service.UserComplaintsService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

/**
 * @description 审核管理
 */
@Service
@Transactional
public class UserComplaintsServiceImpl extends GenericServiceImpl<Usercomplaints, String> implements UserComplaintsService{
	private UserComplaintsRepository repository;

	@Autowired
	public void setRepository(UserComplaintsRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}
	@Override
	public Usercomplaints getByOrderId(String orderId) {
		String sql = "select * from Usercomplaints where orderid=:orderid";
		Map<String, Object> param = new HashMap<>();
		param.put("orderid", orderId);
		List<Usercomplaints> list = this.repository.queryListBySql(sql, param, Usercomplaints.class);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	@Override
	public List<Map<String, Object>> getPic(String orderId) {
		String sql = "select * from UserComplaintsPic where orderid=:orderid order by AppealsPicNo";
		Map<String, Object> param = new HashMap<>();
		param.put("orderid", orderId);
		return this.repository.queryHumpListMapBySql(sql, param);
	}
	@Override
	public List<Map<String, Object>> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.LIST);
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams());
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getQueryCondition(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.* ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from (");
		sql.append(" select a.*,b.undoStatus,c.AppealProblemName,d.AppealProblemTypeName,d.appealProblemTypeID,e.PlatformIntervention");

		sql.append(" from UserComplaints a ");
		sql.append(" inner join TasksOrders b on a.orderid=b.orderid");
		sql.append(" left join AppealStandardProblem c on a.AppealProblemID=c.AppealProblemID");
		sql.append(" left join AppealProblemType d on c.AppealProblemTypeID=d.AppealProblemTypeID");
		sql.append(" left join OrderWhisperRelationship e on e.orderid=a.orderid ");
		sql.append(" ) t");
		sql.append(" where 1=1");
		if (searchMap != null) {

			if (StringUtils.isNotBlank(searchMap.get("userRoles"))) {
				sql.append(" and t.userRoles like :userRoles ");
				params.put("userRoles", "%" + searchMap.get("userRoles").trim() + "%");
			}

			if (StringUtils.isNotBlank(searchMap.get("endAppealsTime"))) {
				sql.append(" and t.AppealsTime <= :endAppealsTime ");
				params.put("endAppealsTime", searchMap.get("endAppealsTime"));
			}
			if (StringUtils.isNotBlank(searchMap.get("startAppealsTime"))) {
				sql.append(" and t.AppealsTime >= :startAppealsTime ");
				params.put("startAppealsTime", searchMap.get("startAppealsTime"));
			}
			if (StringUtils.isNotBlank(searchMap.get("orderId"))) {
				sql.append(" and t.orderId = :orderId ");
				params.put("orderId", searchMap.get("orderId"));
			}
			if (StringUtils.isNotBlank(searchMap.get("appealsStatus"))) {
				sql.append(" and t.appealsStatus = :appealsStatus ");
				params.put("appealsStatus", searchMap.get("appealsStatus"));
			}
			if (StringUtils.isNotBlank(searchMap.get("platformProcessingStatus"))) {
				sql.append(" and t.platformProcessingStatus = :platformProcessingStatus ");
				params.put("platformProcessingStatus", searchMap.get("platformProcessingStatus"));
			}
			if (StringUtils.isNotBlank(searchMap.get("platformIntervention"))) {
				sql.append(" and t.platformIntervention = :platformIntervention ");
				params.put("platformIntervention", searchMap.get("platformIntervention"));
			}
			if (StringUtils.isNotBlank(searchMap.get("undoStatus"))) {
				sql.append(" and t.undoStatus= :undoStatus ");
				params.put("undoStatus", searchMap.get("undoStatus"));
			}
			if (StringUtils.isNotBlank(searchMap.get("appealProblemTypeID"))) {
				sql.append(" and t.appealProblemTypeID= :appealProblemTypeID ");
				params.put("appealProblemTypeID", searchMap.get("appealProblemTypeID"));
			}
		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.AppealsTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}
	@Override
	public long count(Map<String, String> params) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.COUNT);
		return repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams());
	}

}
